package Customer;

import java.util.regex.Pattern;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.time.LocalDateTime;
import java.util.*;
import java.time.LocalDate;

public class Customer extends User {
    private static final int MIN_USERNAME_LENGTH = 5;
    private static final int MIN_PASSWORD_LENGTH = 8;

    private static final Pattern PASSWORD_PATTERN = Pattern.compile(
            "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]).{" + MIN_PASSWORD_LENGTH + ",}$"
    );

    private String email;
    private int failedLoginAttempts;
    private boolean isLocked;

    public Customer(String username, String password, String email) {
        super(username, password);
        this.email = email;
        this.failedLoginAttempts = 0;
        this.isLocked = false;
    }

    public Customer(String username, String password) {
        super(username,password);
    }

    // 在注册期间验证用户名和密码
    public static boolean validateUsername(String username) {
        return username.length() >= MIN_USERNAME_LENGTH;
    }

    public static boolean validatePassword(String password) {
        return PASSWORD_PATTERN.matcher(password).matches();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isLocked() {
        return isLocked;
    }

    public void lockAccount() {
        this.isLocked = true;
    }

    public void unlockAccount() {
        this.isLocked = false;
        this.failedLoginAttempts = 0;
    }

    public void incrementFailedLoginAttempts() {
        this.failedLoginAttempts++;
        if (this.failedLoginAttempts >= 5) {
            lockAccount();
            System.out.println("帐户由于登录尝试失败次数过多而被锁定。");
        }
    }

    public void resetFailedLoginAttempts() {
        this.failedLoginAttempts = 0;
    }

    private String userLevel;
    private LocalDate registrationDate;
    private double totalSpending;
    private String phoneNumber;

    private Map<String, CartItem> cart = new HashMap<>();
    private List<String> purchaseHistory = new ArrayList<>();

    public Customer(String username, String password, String email, String phoneNumber, String userLevel) {
        super(username, password);
        this.email = email;
        this.phoneNumber = phoneNumber;
        this.userLevel = userLevel;
        this.registrationDate = LocalDate.now();
        this.totalSpending = 0.0;
    }

    // 将物品加入购物车
    public void addToCart(Product product, int quantity) {
        if (product.getQuantity() < quantity) {
            System.out.println("产品库存不足" + product.getName());
            return;
        }
        if (cart.containsKey(product.getId())) {
            CartItem item = cart.get(product.getId());
            item.setQuantity(item.getQuantity() + quantity);
        } else {
            cart.put(product.getId(), new CartItem(product, quantity));
        }
        System.out.println("加入 " + quantity + " of " + product.getName() + " 到购物车。");
    }

    // 将物品移除购物车
    public void removeFromCart(String productId) {
        if (cart.containsKey(productId)) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("你确定你要删除id为 " + productId + "的商品? (yes/no): ");
            String confirmation = scanner.nextLine();

            if (confirmation.equalsIgnoreCase("yes")) {
                cart.remove(productId);
                System.out.println("产品已移除购物车。");
            } else {
                System.out.println("操作取消。");
            }
        } else {
            System.out.println("在购物车中未找到商品。");
        }
    }

    // 修改购物车中的物品
    public void modifyCartItem(String productId, int newQuantity) {
        if (cart.containsKey(productId)) {
            CartItem item = cart.get(productId);
            if (newQuantity <= 0) {
                cart.remove(productId);
                System.out.println("Product removed from cart.");
            } else {
                item.setQuantity(newQuantity);
                System.out.println("Cart item quantity updated.");
            }
        } else {
            System.out.println("Product not found in cart.");
        }
    }

    // 结算
    public void checkout(String paymentMethod) {
        double totalAmount = cart.values().stream()
                .mapToDouble(item -> item.getProduct().getPrice() * item.getQuantity())
                .sum();

        System.out.println("用 " + paymentMethod+"结账");
        System.out.println("总钱数: $" + totalAmount);

        // 更新产品库存并添加到购买历史记录
        for (CartItem item : cart.values()) {
            Product product = item.getProduct();
            product.setQuantity(product.getQuantity() - item.getQuantity());
        }
        purchaseHistory.add("Purchased on " + LocalDateTime.now() + ": " + cart.values());
        cart.clear();
        System.out.println("结账成功。");
    }

    // 查看购买历史记录
    public void viewPurchaseHistory() {
        if (purchaseHistory.isEmpty()) {
            System.out.println("未找到历史购买记录。");
        } else {
            for (String history : purchaseHistory) {
                System.out.println(history);
            }
        }
    }

    // Getters and Setters
    public String getUserLevel() {
        return userLevel;
    }

    public void setUserLevel(String userLevel) {
        this.userLevel = userLevel;
    }


    public double getTotalSpending() {
        return totalSpending;
    }

    public void setTotalSpending(double totalSpending) {
        this.totalSpending = totalSpending;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public Map<String, CartItem> getCart() {
        return cart;
    }

    public List<String> getPurchaseHistory() {
        return purchaseHistory;
    }

    @Override
    public String toString() {
        return "用户ID: " + this.getUsername() +
                ", 用户名: " + this.getUsername() +
                ", 用户级别: " + this.userLevel +
                ", 注册日期: " + this.registrationDate +
                ", 总花费: " + this.totalSpending +
                ", 电话: " + this.phoneNumber +
                ", 邮箱: " + this.email;
    }

    private String customerId;
    private LocalDateTime registrationTime;
    private double totalAmountSpent;

    public Customer(String username, String password, String customerId, String userLevel,
                    LocalDateTime registrationTime, double totalAmountSpent,
                    String phoneNumber, String email) {
        super(username, password);
        this.customerId = customerId;
        this.userLevel = userLevel;
        this.registrationTime = registrationTime;
        this.totalAmountSpent = totalAmountSpent;
        this.phoneNumber = phoneNumber;
        this.email = email;
    }

    // Getters and setters
    public String getCustomerId() {
        return customerId;
    }

    public void setCustomerId(String customerId) {
        this.customerId = customerId;
    }

    public LocalDateTime getRegistrationTime() {
        return registrationTime;
    }

    public void setRegistrationTime(LocalDateTime registrationTime) {
        this.registrationTime = registrationTime;
    }

    public double getTotalAmountSpent() {
        return totalAmountSpent;
    }

    public void setTotalAmountSpent(double totalAmountSpent) {
        this.totalAmountSpent = totalAmountSpent;
    }

    public void logout() {
        System.out.println("成功登出！");
    }
}




